package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.repository.TravelCommentMongoRepository;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {
    @Autowired
    private TravelCommentMongoRepository repository;

    //盖楼式评论
    public TravelComment save(TravelComment entity) {

        //处理评论信息
        if (TravelComment.TRAVEL_COMMENT_TYPE.equals(entity.getType())) {//如果当前评论属于回复评论时
            //根据引用评论的id去查原评论信息
            Optional<TravelComment> optional = repository.findById(entity.getRefComment().getId());
            if (optional.isPresent()) {//如果容器中存在
                TravelComment comment = optional.get();//就获取此引用评论
                entity.setRefComment(comment);//再把查到的引用评论设置当当前对象评论中
            }
        }
        entity.setCreateTime(new Date());
        //设置引用对象的信息
        repository.save(entity);
        return entity;
    }

    public List<TravelComment> listByTravelId(Long id) {
        return repository.findByTravelId(id);
    }
}
